package view;

import java.lang.reflect.Field;
import java.util.Collection;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import interfaces.Controller;
import interfaces.CollectionView;
import interfaces.LabelClick;

public class PropertyView implements CollectionView{

	@Override
	public JPanel getView(Collection<?> collection, JFrame window, Controller controller) {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		for (Object object : collection) {
			JLabel label = new JLabel();
			if(!object.getClass().isPrimitive() && !object.getClass().equals(String.class) && !object.getClass().equals(Integer.class) && !object.getClass().equals(Double.class) && !object.getClass().equals(Boolean.class)){
				label.addMouseListener(new LabelClick(object, window, controller));
				label.setText(allFields(object));
			}else{
				label.setText(object.toString());
			}
			panel.add(label);
		}
		return panel;
	}
	
	
	public String allFields(Object object){
		String string = "";
		try {
			Class<?> c = object.getClass();
			for (Field f : c.getDeclaredFields()) {
				f.setAccessible(true);
				Object o = f.get(object);
				string += o + " | ";
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return string;
	}
}
